package model;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * Server Representation of the gameboard
 * 
 * @author s3222417
 * 
 */
public class GameBoard
{
	private Hashtable<String, GameTile> board;
	private int height;
	private int width;
	private ArrayList<Player> playerList;
	final int MAX_PLAYERS = 4;

	public GameBoard(int height, int width)
	{
		super();
		this.height = height;
		this.width = width;
		board = new Hashtable<String, GameTile>();
		playerList = new ArrayList<Player>();

	}

	public void setBoardUsingList(ArrayList<GameTile> list)
	{
		board = new Hashtable<String, GameTile>();
		for (GameTile gameTile : list)
		{
			board.put(gameTile.getxCoord() + "*" + gameTile.getyCoord(),
					gameTile);
		}
	}

	public GameBoard()
	{
		height = 9;
		width = 9;
		board = new Hashtable<String, GameTile>();
	}

	public void generateBoard()
	{
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				try
				{
					addTilebyCoordinate(new GameTile(true, true, i, j));
				}
				catch (Exception e)
				{
					// TODO Auto-generated catch block
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
		}
	}

	public void addTilebyCoordinate(GameTile gameTile) throws Exception
	{
		if (gameTile.getxCoord() < 0 || gameTile.getyCoord() < 0
				|| gameTile.getxCoord() >= width
				|| gameTile.getyCoord() >= height)
		{
			throw new Exception(
					"X or Y coordinate is invalid please enter an integer 0 or above or within width and height of board.");
		}

		String tileKey = new String(gameTile.getxCoord() + "*"
				+ gameTile.getyCoord());

		board.put(tileKey, gameTile);

	}

	public void addPlayer(Player player) throws Exception
	{
		if (playerList.size() < MAX_PLAYERS)
		{
			playerList.add(player);
		}
		else
		{
			throw new Exception(
					"Maximum players in game, cannot add more players");
		}
	}

	public GameTile getTilebyCoordinate(int x, int y)
	{
		return board.get(x + "*" + y);

	}

	public Hashtable<String, GameTile> getBoard()
	{
		return board;
	}

	public void setBoard(Hashtable<String, GameTile> board)
	{
		this.board = board;
	}

	public int getHeight()
	{
		return height;
	}

	public void setHeight(int height)
	{
		this.height = height;
	}

	public int getWidth()
	{
		return width;
	}

	public void setWidth(int width)
	{
		this.width = width;
	}

}
